package com.anjubao.parking.municipal.roadside.controller;

import com.alibaba.fastjson.JSONObject;
import com.anjubao.parking.municipal.common.utils.BeanUtils;
import com.anjubao.parking.municipal.common.utils.U;
import com.anjubao.parking.municipal.roadside.constant.CommandConstants;
import com.anjubao.parking.municipal.roadside.constant.DeviceStatusEnum;
import com.anjubao.parking.municipal.roadside.dto.DefaultResponseDto;
import com.anjubao.parking.municipal.roadside.dto.DeviceRegisterResultDto;
import com.anjubao.parking.municipal.roadside.dto.cmd.ImgreportCmdDto;
import com.anjubao.parking.municipal.roadside.form.AlarmReportForm;
import com.anjubao.parking.municipal.roadside.form.DeviceRegisterForm;
import com.anjubao.parking.municipal.roadside.form.HeartbeatForm;
import com.anjubao.parking.municipal.roadside.form.ImgreportForm;
import com.anjubao.parking.municipal.roadside.model.DeviceStatus;
import com.anjubao.parking.municipal.roadside.service.DeviceApiService;
import com.anjubao.parking.municipal.roadside.service.DeviceStatusService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.ParseException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 视频桩上报
 *
 * @author zhuchunfeng
 * @date 2019/6/13 15:02
 */

@RestController
@RequestMapping("/parking-roadside-newapi")
@Slf4j
public class DeviceApiController {
    @Autowired
    private DeviceApiService deviceApiService;


    @Autowired
    private DeviceStatusService deviceStatusService;

    /**
     * 车场信息（1：新增，2：修改）
     * @return
     */
    @PostMapping(value = "/login")
    @ApiOperation(value = "设备登录操作", notes = "", code = 2000)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", required = true, dataType = "DeviceRegisterForm", paramType = "body")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    public Object DeviceRegister(@RequestBody @Valid DeviceRegisterForm deviceRegisterForm){
        log.info("POST 登录 收到登录url 回复*******************************" + deviceRegisterForm);

        if (deviceRegisterForm.getCmd().equals(CommandConstants.LOGIN)) {
            log.info("设备登录注册---------------");
            log.info(U.json(deviceRegisterForm));
            return deviceApiService.handleLogin(deviceRegisterForm);

        } else {
            log.info("收到心跳 回复********************");
            log.info(U.json(deviceRegisterForm));
            return new DefaultResponseDto();
        }
    }

    @PostMapping("/heartbeat")
    public Object heartbeat(@RequestBody HeartbeatForm heartbeatForm) {

        if (heartbeatForm.getCmd().equals(CommandConstants.HEARTBEAT)) {
            log.info("心跳: {}",U.json(heartbeatForm));
            return deviceApiService.handleHeartbeat(heartbeatForm);
        } else {
            log.info("心跳 未知请求 {}",U.json(heartbeatForm));
            return new DefaultResponseDto();
        }
    }

    @PostMapping("/imgreport")
    public Object imgreport(@RequestBody ImgreportForm imgreportForm) {

        if (imgreportForm.getCmd().equals(CommandConstants.IMGREPORT)) {
            log.info("第一个停车上报---------------");
            LocalDateTime dateTime=LocalDateTime.now();
//            log.info(U.json(imgreportForm));
            Duration duration = java.time.Duration.between(dateTime,  LocalDateTime.now() );
            try {
                Object object=deviceApiService.handleImgreport(imgreportForm);
                log.info("返回时间------"+duration.toMillis()+"-----"+U.json(object));
                return object;
            } catch (ParseException e) {
                ImgreportCmdDto imgreportCmdDto = new ImgreportCmdDto();
                imgreportCmdDto.setDevtype(imgreportForm.getDevtype());
                imgreportCmdDto.setDevid(imgreportForm.getDevid());
                imgreportCmdDto.setCmd(imgreportForm.getCmd());
                imgreportCmdDto.setId(imgreportForm.getId());
                imgreportCmdDto.setResponse("0");
                return imgreportCmdDto;
            }

        } else if(imgreportForm.getCmd().equals(CommandConstants.PLATEREPORT)){
            log.info("收到车牌纠正 回复********************");
            log.info(U.json(imgreportForm));
            return new DefaultResponseDto();
        }else {
            log.info("收到心跳 回复********************");
            log.info(U.json(imgreportForm));
            return new DefaultResponseDto();
        }
    }

    @PostMapping("/alarm")
    public Object alarm(@RequestBody AlarmReportForm alarmReportForm) {
        if (alarmReportForm.getCmd().equals(CommandConstants.ALARMREPORT)) {
            log.info("手动抓拍回复------------------"+U.json(alarmReportForm));
            //处理图片
            return deviceApiService.handleAlarmReport(alarmReportForm);
        }
        log.info("收到报警url 回复******************************"+U.json(alarmReportForm));
        return new DefaultResponseDto();

    }



    //监控备份接口
    @PostMapping(value = "/login/backup")
    @ApiOperation(value = "登录备份URL")
    public Object DeviceRegister_(@RequestBody JSONObject jsonObject){

        log.info("登录 备份请求: {}",jsonObject.toJSONString());
        return this.DeviceRegister(jsonObject.toJavaObject(DeviceRegisterForm.class));
    }

    @PostMapping("/heartbeat/backup")
    @ApiOperation(value = "心跳备份URL")
    public Object heartbeat_(@RequestBody JSONObject jsonObject) {

        log.info("心跳 备份请求: {}",jsonObject.toJSONString());
        return this.heartbeat(jsonObject.toJavaObject(HeartbeatForm.class));
    }

    @PostMapping("/imgreport/backup")
    @ApiOperation(value = "数据备份URL")
    public Object imgreport_(@RequestBody JSONObject jsonObject) {

        log.info("数据 备份请求: {}",jsonObject.toJSONString());
        return this.imgreport(jsonObject.toJavaObject(ImgreportForm.class));
    }

    @PostMapping("/alarm/backup")
    @ApiOperation(value = "报警备份URL")
    public Object alarm_(@RequestBody JSONObject jsonObject) {

        log.info("报警 备份请求: {}",jsonObject.toJSONString());
        return this.alarm(jsonObject.toJavaObject(AlarmReportForm.class));
    }



}
